<template>
  <el-dialog
    :title="!dataForm.urId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    append-to-body>
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户id" prop="urUserId">
      <el-input v-model="dataForm.urUserId" placeholder="用户id"></el-input>
    </el-form-item>
    <el-form-item label="角色id" prop="urRoleId">
      <el-input v-model="dataForm.urRoleId" placeholder="角色id"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          urId: 0,
          urUserId: '',
          urRoleId: ''
        },
        dataRule: {
          urUserId: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],
          urRoleId: [
            { required: true, message: '角色id不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.urId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.urId) {
            this.$http({
              url: this.$http.adornUrl(`/fiction/tbuserrolerelevance/info/${this.dataForm.urId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then((data) => {
              if (data && data.code === 200) {
                this.dataForm.urUserId = data.tbUserRoleRelevance.urUserId
                this.dataForm.urRoleId = data.tbUserRoleRelevance.urRoleId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/fiction/tbuserrolerelevance/${!this.dataForm.urId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'urId': this.dataForm.urId || undefined,
                'urUserId': this.dataForm.urUserId,
                'urRoleId': this.dataForm.urRoleId
              })
            }).then((data) => {
              if (data && data.code === 200) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
